﻿using DevExpress.Xpf.Core.Native;
using PlaceOrder_Admin.ViewModels;
using Prism.DryIoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlaceOrder_Admin.Views
{
    /// <summary>
    /// ChatWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChatAdminWindow : System.Windows.Controls.UserControl
    {
        public ChatAdminWindow()
        {
            InitializeComponent();


            Loaded += async (s, e) =>
            {
                await Task.Delay(100); // 等待一段时间确保 DataContext 设置完毕
                var viewModel = (ChatAdminWindowViewModel)this.DataContext;
                if (viewModel != null)
                {
                    viewModel.ScrollToBottomRequested += (sender, args) => ScrollToBottom();
                    viewModel.ScrollToBottom();

                }
            };
        }

        private void ScrollToBottom()
        {
            PrismApplication.Current.Dispatcher.Invoke(() =>
            {
                if (ListBoxChat.Items.Count > 0)
                {
                    var lastItem = ListBoxChat.Items[ListBoxChat.Items.Count - 1];
                    ListBoxChat.ScrollIntoView(lastItem);
                }
            });
        }


    }
}
